package br.com.guiav6.sisguia.component;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import br.com.guiav6.sisguia.utils.MessagesUtil;
import br.com.guiav6.sisguia.utils.PropertiesUtil;

public class Action {
	
    protected String getParameter(String param) {
        return FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get(param);
    }
    
    protected String[] getParameterValues(String param) {
        return ((HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest()).getParameterValues(param);
    }
    
    protected void setParameterValues(String param, Object value) {
        ((HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest()).setAttribute(param, value);
    }
    
    protected Object getSessionParam(String paramName) {
        FacesContext context = FacesContext.getCurrentInstance();
        return context.getExternalContext().getSessionMap().get(paramName);
    }
    
    
    protected Object setSessionParam(String paramName, Object paramValue) {
        FacesContext context = FacesContext.getCurrentInstance();
        return context.getExternalContext().getSessionMap().put(paramName, paramValue);
    }
    
    protected void removeSessionParam(String paramName) {
        FacesContext context = FacesContext.getCurrentInstance();
        context.getExternalContext().getSessionMap().remove(paramName);
    }
    
    protected void sessionInvalidate() {
        FacesContext context = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
        session.invalidate();
    }
    
    private String getMessage(String message) {
        return MessagesUtil.getMessage(message);
    }
    
    protected void addErrorMessage(String idCampo, String msg) {
        FacesContext.getCurrentInstance().addMessage(idCampo, new FacesMessage(FacesMessage.SEVERITY_ERROR, getMessage(msg), null));
    }
    
    protected void addWarnMessage(String idCampo, String msg) {
        FacesContext.getCurrentInstance().addMessage(idCampo, new FacesMessage(FacesMessage.SEVERITY_WARN, getMessage(msg), null));
    }
    
    protected void addInfoMessage(String idCampo, String msg) {
        FacesContext.getCurrentInstance().addMessage(idCampo, new FacesMessage(FacesMessage.SEVERITY_INFO, getMessage(msg), null));
    }
    
    protected void addFatalMessage(String idCampo, String msg) {
        FacesContext.getCurrentInstance().addMessage(idCampo, new FacesMessage(FacesMessage.SEVERITY_FATAL, getMessage(msg), null));
    }
    
    protected String navigate(String key) {
        return PropertiesUtil.getInstance().setBundle("navigation").getProperty(key);
    }
    
}
